import 'package:BeeApp/CarPage/MainCarUserPage.dart';
import 'package:BeeApp/MainPage/MainOldOrderPage.dart';
import 'package:BeeApp/MainPage/MainPursePage.dart';
import 'package:BeeApp/LoginAndRegister/LoginModelPage.dart';
import 'package:BeeApp/LoginAndRegister/LoginPage.dart';
import 'package:BeeApp/MainPage/MainModelPage.dart';
import 'package:BeeApp/MainPage/MainSetModel.dart';
import 'package:BeeApp/MainPage/MainShowHTMLPage.dart';
import 'package:BeeApp/MainPage/MainToolPage.dart';
import 'package:BeeApp/MainPage/http_request.dart';
import 'package:BeeApp/MainPage/MainKeFuPage.dart';
import 'package:BeeApp/ShopPage/MainUserPage.dart';
import 'package:BeeApp/ShopPage/UserManagePage.dart';
import 'package:BeeApp/MainPage/MainGGPage.dart';
import 'package:BeeApp/ShopPage/UserRealNamePage.dart';
import 'package:BeeApp/MainPage/MainNewsChangePage.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class UserNewsPage extends StatefulWidget {
  UserNewsPage({Key key}) : super(key: key);

  @override
  _UsetNewsPageState createState() => _UsetNewsPageState();
}

class _UsetNewsPageState extends State<UserNewsPage> {
  LoginModel loginModel;
  UserModel userModel;
  MainSetModel mainSetModel;
  String nameStr = getData("nickname");
  String imageStr;
  @override
  void initState() {
    super.initState();
    print(getData("nickname"));
    upLoadData();
    uploadSet();
  }

  void upLoadData() {
    DioUtils.postHttp(
      context,
      "Usercommon/user_info",
      parameters: {"token": getData("token")},
      onSuccess: (t) {
        print(t);
        MainModel mainmodel = MainModel.fromJson(t);
        if (mainmodel.code == 1) {
          if (mounted) {
            setState(() {
              userModel = UserModel.fromJson(t["data"]);
              nameStr = userModel.nickname;
              setData("nickname", userModel.nickname);
              imageStr = userModel.avatar;
              setData("avatar", userModel.avatar);
              setData("sex", userModel.sex);
              setData("money", userModel.money);
            });
          }
        }
      },
      onError: (error) {
        print(error);
      },
    );
  }

  void uploadSet() {
    DioUtils.noTokenPostHttp(
      context,
      "Usercommon/app_config",
      onSuccess: (t) {
        print(t);
        MainModel model = MainModel.fromJson(t);
        if (model.code == 1 && model.data != null) {
          if (mounted) {
            setState(() {
              mainSetModel = MainSetModel.fromJson(t);
            });
          }
        }
      },
      onError: (error) {
        print(error);
      },
    );
  }

  Widget nextImage() {
    return Image.asset(
      "images/MainNextImage.png",
      height: setHeight(30),
      // width: ScreenUtil().setHeight(200),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      // appBar: getAppBar(context, "个人中心"),
      appBar: AppBar(
        title: Text("个人中心"),
        elevation: 0,
        centerTitle: true,
        leading: IconButton(
          icon: Image.asset(
            "images/MainBackImage.png",
            height: 20,
            width: 20,
            color: Colors.black,
          ),
          onPressed: () {
            Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(builder: (context) => MainUserPage()),
                (route) => false);
          },
        ),
      ),
      body: ListView(
        children: <Widget>[
          SizedBox(height: 1),
          InkWell(
            child: userNewsView(),
            onTap: () {
              print("object");
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => MainNewsChangePage()),
              ).then((value) => {
                    upLoadData(),
                  });
            },
          ),
          imageView(),
          Container(
            padding: EdgeInsets.fromLTRB(20, 0, 20, 20),
            child: Column(
              children: <Widget>[
                otherView("images/QianBaoImage.png", "我的钱包", 1),
                SizedBox(height: 1),
                otherView("images/NewsListImage.png", "消息管理", 2),
                SizedBox(height: 1),
                otherView("images/RealNameImage.png", "实名认证", 3),
                SizedBox(height: 1),
                otherView("images/KeFuImage.png", "客服中心", 4),
                SizedBox(height: 1),
                otherView("images/OldOrderImage.png", "历史订单", 5),
                SizedBox(height: 1),
                otherView("images/GongGaoImage.png", "平台公告", 6),
                SizedBox(height: 1),
                otherView("images/AboutMeImage.png", "关于我们", 7),
                SizedBox(height: 1),
                otherView("images/XieYiImage.png", "用户协议和隐私政策", 8),
                SizedBox(height: setHeight(30)),
                changeView(),
                SizedBox(height: setHeight(30)),
                btnView()
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget userNewsView() {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.fromLTRB(20, 10, setWidth(60), 10),
      child: Row(
        children: [
          ClipOval(
            child: imageStr == null
                ? Image.asset(
                    "images/MainLogoImage.png",
                    height: setHeight(100),
                    width: setHeight(100),
                  )
                : CachedNetworkImage(
                    imageUrl: imageStr,
                    height: setHeight(100),
                    width: setHeight(100),
                    fit: BoxFit.cover),
          ),
          SizedBox(width: 10),
          Expanded(
            child: Text(
              nameStr,
              style: TextStyle(
                  color: Color(0xFF333333), fontSize: setFontSize(34)),
            ),
          ),
          SizedBox(width: 5),
          nextImage(),
        ],
      ),
    );
  }

  Widget imageView() {
    return Container(
      padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
      child: InkWell(
        child: Container(
          height: setHeight(120),
          width: 900,
          decoration: BoxDecoration(
            color: Colors.yellow,
            borderRadius: BorderRadius.all(Radius.circular(10.0)),
            // image: DecorationImage(image: NetworkImage("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2692809760,3266574367&fm=26&gp=0.jpg"),
            // fit: BoxFit.cover)
            image: DecorationImage(
                image: AssetImage("images/zhaomu.png"), fit: BoxFit.cover),
          ),
        ),
        onTap: () {
          print("object");
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) =>
                      MainShowHTMLPage("车主招募", mainSetModel.data.carRecruit)));
        },
      ),
    );
  }

  BorderRadius radius(int index) {
    if (index == 1) {
      return BorderRadius.only(
          topLeft: Radius.circular(10.0), topRight: Radius.circular(10.0));
    } else if (index == 8) {
      return BorderRadius.only(
          bottomLeft: Radius.circular(10.0),
          bottomRight: Radius.circular(10.0));
    } else {
      return BorderRadius.all(Radius.circular(0.0));
    }
  }

  Widget otherView(
    String imageStr,
    String str,
    int index,
  ) {
    return InkWell(
      child: Container(
        height: 50,
        padding: EdgeInsets.only(left: setWidth(30), right: setWidth(30)),
        decoration: BoxDecoration(
          color: Colors.white,
          // borderRadius: BorderRadius.all(Radius.circular(10.0)),
          borderRadius: radius(index),
        ),
        child: Row(
          children: [
            Image.asset(
              imageStr,
              width: 30,
            ),
            SizedBox(width: setWidth(7)),
            Expanded(
              child: Text(
                str,
                style: TextStyle(
                  fontSize: setFontSize(30),
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
            SizedBox(width: 5),
            nextImage(),
          ],
        ),
      ),
      onTap: () {
        print("object" + index.toString());
        pushPage(index);
      },
    );
  }

  pushPage(int index) {
    if (index == 1) {
      // 我的钱包
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => CarPursePage()));
    } else if (index == 2) {
      // 消息管理
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => UserManagePage()));
    } else if (index == 3) {
      // 实名认证
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => UserRealNamePage()));
    } else if (index == 4) {
      // 客服中心
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => KefuPage()));
    } else if (index == 5) {
      // 历史订单
      Navigator.push(context,
          MaterialPageRoute(builder: (context) => MainOldOrderPage(true)));
    } else if (index == 6) {
      // 平台公告
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => MainGGPage()));
    } else if (index == 7) {
      // 关于我们
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) =>
                  MainShowHTMLPage("关于我们", mainSetModel.data.aboutUs)));
    } else if (index == 8) {
      // 用户协议和隐私政策
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => MainShowHTMLPage(
                  "用户协议和隐私政策", mainSetModel.data.agreementPolicy)));
    } else {}
  }

  Widget changeView() {
    return InkWell(
      child: Container(
        height: 50,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Color(0xFFFF8629),
            borderRadius: BorderRadius.all(Radius.circular(12.0)),
            border: Border.all(color: Color(0xFF333333), width: setWidth(3))),
        child: Text(
          "切换为车主",
          style: TextStyle(
            color: Color(0xFF222222),
            fontSize: setFontSize(34),
            fontWeight: FontWeight.w600,
          ),
        ),
      ),
      onTap: () {
        print("object");
        showAlertDialog(context, "2");
      },
    );
  }

  Widget btnView() {
    return InkWell(
      child: Container(
        height: 50,
        width: 900,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(10.0)),
        ),
        child: Text(
          "退出登录",
          style: TextStyle(
            color: Color(0xFF333333),
            fontSize: setFontSize(32),
          ),
        ),
      ),
      onTap: () {
        print("object");
        showAlertDialog(context, "1");
      },
    );
  }

  showAlertDialog(BuildContext context, String index) {
    //显示对话框
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(index == "1" ? "确定要退出吗?" : "确定要切换为车主吗?"),
          // content: Text("确定要退出吗?"),
          // 设置成 圆角
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          actions: [
            FlatButton(
              child: Text(
                "取消",
                style: TextStyle(
                  fontSize: setFontSize(30),
                ),
              ),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            FlatButton(
              child: Text(
                "确认",
                style: TextStyle(
                  color: Colors.orange,
                  fontSize: setFontSize(30),
                ),
              ),
              onPressed: () {
                setData("AppType", index);

                if (index == "1") {
                  setData("loginType", "0");
                  Navigator.pushAndRemoveUntil(
                      context,
                      MaterialPageRoute(builder: (context) => LoginPage()),
                      (route) => false);
                } else {
                  setData("loginType", "1");
                  DioUtils.postHttp(
                    context,
                    "usercenter/user_login",
                    parameters: {"token": getData("token"), "user_type": "2"},
                    onSuccess: (t) {
                      print(t);
                      loginModel = LoginModel.fromJson(t);
                      if (loginModel.code == 1) {
                        setData("id", loginModel.data.userinfo.id);
                        setData("token", loginModel.data.userinfo.token);
                        setData("user_type", loginModel.data.userinfo.userType);
                        setData("avatar", loginModel.data.userinfo.avatar);
                        setData("nickname", loginModel.data.userinfo.nickname);
                        setData("mobile", loginModel.data.userinfo.mobile);
                        setData("sex", loginModel.data.userinfo.sex);
                        Navigator.pushAndRemoveUntil(
                            context,
                            MaterialPageRoute(
                                builder: (context) => MainCarUserPage()),
                            (route) => false);
                      } else {
                        showToast(t["msg"]);
                      }
                    },
                    onError: (error) {
                      print(error);
                    },
                  );
                }
              },
            ),
          ],
        );
      },
    );
  }
}
